package com.example.forum2.Controller;

import com.example.forum2.Service.impl.ArticleServiceImpl;
import com.example.forum2.comment.AppResult;
import com.example.forum2.comment.ResultCode;
import com.example.forum2.config.AppConfig;
import com.example.forum2.exception.ApplicationExeception;
import com.example.forum2.model.Article;
import com.example.forum2.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleServiceImpl articleService;
   @RequestMapping ("/create")
    public AppResult create (HttpServletRequest request, @RequestParam(value =
            "boardId")  Long boardId,  @RequestParam(value =
            "title")  String title,  @RequestParam(value =
            "content")  String content) {

        HttpSession session = request.getSession();

        User user = (User) session.getAttribute(AppConfig.USER_SESSION);

        if (user.getState() == 1) {

            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }

        Article article = new Article();
        article.setBoardId(boardId);
        article.setUserId(user.getId());
        article.setTitle(title);
        article.setContent(content);

        articleService.create(article);

        return AppResult.success();
    }
//    @RequestMapping("/getAllByBoardId")
//    public AppResult<List<Article>> getAllByBoardId(Long boardId){
//       List<Article> articles = articleService.selectAll();
//       if(articles==null){
//           articles= new ArrayList<>();
//       }
//       return AppResult.success(articles);
//    }
    @RequestMapping("/getAllByBoardId")
    public AppResult<List<Article>> getAllBoardId(Long boardId){
       List<Article> articles;
       if(boardId==null){
           articles=articleService.selectAll();
       }else{
            articles = articleService.selectAllByBoardId(boardId);
       }
       if(articles==null){
           articles=new ArrayList<>();
       }
       return AppResult.success(articles);

    }
    @RequestMapping("/details")
public AppResult<Article> getDetails(Long id,HttpServletRequest request){
       HttpSession session = request.getSession();
       User user = (User) session.getAttribute(AppConfig.USER_SESSION);
       Article article = articleService.selectDetailById(id);
       if(article==null){
           return AppResult.failed(ResultCode.FAILED);
       }
       if(user.getId()==article.getUserId()){
           article.setOwn(true);
       }
       return AppResult.success(article);
}
@RequestMapping("/modify")
public AppResult modify(HttpServletRequest httpServletRequest,Long id,String title,String content){
       HttpSession session = httpServletRequest.getSession();
       User user = (User) session.getAttribute(AppConfig.USER_SESSION);
       if(user.getState() != 0){
           return AppResult.failed(ResultCode.FAILED_USER_BANNED);
       }
       Article article = articleService.selectById(id);
       if(article==null){
           return AppResult.failed(ResultCode.FAILED_USER_BANNED);
       }
       if(article.getUserId()!=user.getId()){
           return AppResult.failed(ResultCode.FAILED_USER_BANNED);
       }
       return AppResult.success(article);
}
@RequestMapping("/thumbsUp")
public AppResult thumbsup(Long id,HttpServletRequest request){
       HttpSession  session = request.getSession();
       User user = (User)  session.getAttribute(AppConfig.USER_SESSION);
       if(user.getId()==1){
           return AppResult.failed(ResultCode.FAILED_USER_BANNED);
       }
       articleService.thumbsUpById(id);
       return AppResult.success();
}
@RequestMapping("/delete")
    public AppResult deleteById(HttpServletRequest request,Long id){
        HttpSession session = request.getSession(false);
        User user=(User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState()==1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        Article article = articleService.selectById(id);
      //  article.setDeleteState((byte)0);
        if(article==null||article.getDeleteState()==1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        if(user.getId()!=article.getUserId()){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        articleService.deleteById(id);
        return AppResult.success();
    }
    @RequestMapping("/getAllByUserId")
    public AppResult getAllByUserId(HttpServletRequest request,Long boardId){
       if(boardId==null){
           HttpSession session = request.getSession();
           User user =(User) session.getAttribute(AppConfig.USER_SESSION);
           boardId = user.getId();
       }
        List<Article> list = articleService.selectByUserId(boardId);
       return AppResult.success(list);
    }

}

